# -*- coding:utf-8 -*-
"""
交易数据接口
Created on 2021/09/18
@author: litiansheng
@group : lts
@contact: ltsjim@163.com
"""
from __future__ import division
import os
import sys
import time
import json
import lxml.html
# from lxml import etree
import pandas as pd
import numpy as np
import datetime
import cons as ct
import re
import wencaicopy
import chajian
import dfcfcopy
# from pandas.compat import StringIO
# from tushare.util import dateu as du
# from tushare.util.formula import MA
# import os
# from tushare.util.conns import get_apis, close_apis
# from tushare.stock.fundamental import get_stock_basics

BASE = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, BASE)
import com

'''————————————————
版权声明：本文为CSDN博主「xsophiax」的原创文章，遵循CC
4.0
BY - SA版权协议，转载请附上原文出处链接及本声明。
原文链接：https: // blog.csdn.net / xsophiax / article / details / 106613508'''

try:
    from urllib.request import urlopen, Request
except ImportError:
    from urllib2 import urlopen, Request
v = pd.__version__
if int(v.split('.')[1]) >= 25 or int(v.split('.')[0]) > 0:
    from io import StringIO
else:
    from pandas.compat import StringIO


def _parsing_dayprice_json(types=None, page=1):
    """
           处理当日行情分页数据，格式为json
     Parameters
     ------
        pageNum:页码
     return
     -------
        DataFrame 当日所有股票交易数据(DataFrame)
    """
    ct._write_console()
    request = Request(ct.SINA_DAY_PRICE_URL % (ct.P_TYPE['http'], ct.DOMAINS['vsf'],
                                               ct.PAGES['jv'], types, page))
    text = urlopen(request, timeout=10).read()
    #print(text)
    if text == 'null':
        return None
    reg = re.compile(r'\,(.*?)\:')
    text = reg.sub(r',"\1":', text.decode('gbk') if ct.PY3 else text)
    text = text.replace('"{"symbol', '{"symbol')
    text = text.replace('{symbol', '{"symbol"')
    text = text.replace('""', '"')
    if ct.PY3:
        jstr = json.dumps(text)
    else:
        jstr = json.dumps(text, encoding='GBK')
    js = json.loads(jstr)
    df = pd.DataFrame(pd.read_json(js, dtype={'code': object}),
                      columns=ct.DAY_TRADING_COLUMNS)
    df = df.drop('symbol', axis=1)
    #     df = df.ix[df.volume > 0]
    return df


def get_today_all():
    """
        一次性获取最近一个日交易日所有股票的交易数据
    return
    -------
      DataFrame
           属性：代码，名称，涨跌幅，现价，开盘价，最高价，最低价，最日收盘价，成交量，换手率，成交额，市盈率，市净率，总市值，流通市值
    """
    ct._write_head()
    df = _parsing_dayprice_json('hs_a', 1)
    if df is not None:
        for i in range(2, ct.PAGE_NUM[1]):
            time.sleep(2)
            newdf = _parsing_dayprice_json('hs_a', i)
            if newdf.shape[0] > 0:
                df = df.append(newdf, ignore_index=True)
            else:
                break
    df = df.append(_parsing_dayprice_json('shfxjs', 1),
                   ignore_index=True)
    return df


def get_today_all_dfcf(num=200):
    df=dfcfcopy.Get_Data(num)
    """->dataframe
    #    f1    f10   f11    f115     f12 f128  f13 f136    f14 f140 f141     f15  f152     f16     f17     f18      f2          f20          f21   f22    f23     f24     f25      f3     f4       f5            f6          f62     f7     f8      f9
0    2      - -0.18   62.04  688697    -    1    -    N纽威    -    -   29.44     2   22.31   28.00    7.55   22.47   7340200749   1493102199  0.04   6.42  197.62  197.62  197.62  14.92   478049  1.161526e+09  101859063.0  94.44  71.94   53.17
1    2      -  0.00   29.91  605598    -    1    -    N港湾    -    -   19.97     2   16.64   16.64   13.87   19.97   3449687036    862573536  0.00   2.47   43.98   43.98   43.98   6.10    10224  2.038750e+07   18788374.0  24.01   2.37   34.54
2    2   2.46  0.00   28.47  300610    -    0    -   晨化股份    -    -   22.60     2   18.27   18.40   18.83   22.60   4811020200   3599955921  0.00   4.90   89.76   99.47   20.02   3.77   591681  1.292781e+09  267805774.0  23.00  37.14   27.63
3    2   2.58  0.00   28.38  301049    -    0    -   超越科技    -    -   41.74     2   38.39   38.39   34.78   41.74   3934134161    932646157  0.00   4.53  115.82  115.82   20.01   6.96    98182  4.049137e+08  128749066.0   9.63  43.94   48.97

    """
    df=df.rename(columns={'f2':'最新价',
                             'f3': '涨跌幅',
                             'f4': '涨跌额',
                             'f5': '成交量',
                             'f6': '成交额',
                             'f7': '振幅',
                             'f8': '换手率',
                             'f12': 'code',
                             'f14': '名称',
                             'f15': '最高',
                             'f16': '最低',
                             'f17': '今开',
                             'f18': '昨收',
                             'f13': '市净率'
                             })
    # f2最新价 f3涨跌幅 f4涨跌额 f5成交量 f6成交额 f7振幅  f8换手率 f12 代码  f14 名称  f15最高价  f16最低  f17今开 f18昨收  f23 市净率
    return df[["code","名称","最新价","成交量","换手率","最高","最低","今开"]]

def searchtosave(func):
    def wrapper(*args,**kwargs):
        res=func(*args,**kwargs)
        path=os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
        time_=com.time_saveandget()
        if "250天" in args[0]:

            path = os.path.join(path,"db","tushare_his","250day", '{}.json'.format(time_))
            print("->开始保存到tushare_his 250day")
            res.to_csv("{}".format(path), encoding="utf-8")
            return res
        if "成交量" in args[0]:
            path = os.path.join(path,"db","tushare_his","MA120", '{}.json'.format(time_))
            print("->开始保存到tushare_his MA120")
            res.to_csv("{}".format(path), encoding="utf-8")
            return res
        if "人气" in args[0]:
            path = os.path.join(path,"db","tushare_his","hot_rank", '{}.json'.format(time_))
            print("->开始保存到tushare_his hot_rank")
            res.to_csv("{}".format(path), encoding="utf-8")
            return res
        else:
            path = os.path.join(path,"db","tushare_his","other", '{}{}.json'.format(args[0],time_))
            print("->开始保存到tushare_other")
            res.to_csv("{}".format(path), encoding="utf-8")
            return res

    return wrapper

@searchtosave
def search(query_string,*wargs):
    #函数在wencaicopy里面
    print("正在查找 1.人气   2.250天均线且换手率大于3   3.成交量 ")
    df = wencaicopy.searchMain(query_string,*wargs)

    if "250天"in query_string:
        #print(df.info())
        df_return = chajian.dftore(df)
        return df_return
    """
###       code market_code  个股热度[20210920]      最新价   最新涨跌幅       股票代码   股票简称
0    601318          17        191045.5    48.68  -1.676  601318.SH   中国平安
1    600905          17        109956.0     6.62   9.967  600905.SH   三峡能源
2    300772          33         70655.0    57.84  20.000  300772.SZ   运达股份  
    """
    if ("人气" or "热股") in query_string :

        #print(df[["code","股票简称","最新价","最新涨跌幅"]])
        return df[["code","股票简称","最新价","最新涨跌幅"]]
    if "成交量" in query_string :
        return df
    return df


if __name__ == '__main__':
    # print(get_today_all())
    # a=search("20210706价格大于250天均线且换手率大于3")
    # print(a)
    # request = Request('%sdata.%s/DataCenter_V3/stock2016/TradeDetail/pagesize=200,page=1,sortRule=-1,sortType=,startDate=%s,endDate=%s,gpfw=0,js=vardata_tab_1.html' % ('http', 'eastmoney.com', date, date))
    # text = urlopen(request, timeout=10).read()
    # import tushare
    #get_today_all_dfcf(2)
    print(search("20210706价格大于250天均线且换手率大于3"))

